<template>
  <!-- 地区销量趋势 -->
  <div class="com-container">
    <div class="title">
      <el-dropdown @command="handleCommand" :style="comStyle">
        <span class="el-dropdown-link">
          ▎{{ select.text
          }}<i class="el-icon-arrow-down el-icon--right" :style="comStyle"></i>
        </span>
        <el-dropdown-menu slot="dropdown" :style="comStyle">
          <el-dropdown-item
            v-for="item in selectType"
            :key="item.key"
            :command="item"
            :style="comStyle"
            >{{ item.text }}</el-dropdown-item
          >
        </el-dropdown-menu>
      </el-dropdown>
    </div>
    <div class="com-chart" ref="trend"></div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      chartInstance: null, //Charts实例
      allData: null,
      select: {
        key: "map",
        text: "地区销量趋势",
      },
      titleFontSize: 0, // 指明标题的字体大小
    };
  },
  computed: {
    selectType() {
      if (!this.allData) {
        return [];
      } else {
        return this.allData.type.filter((item) => {
          return item.key !== this.select.key;
        });
      }
    },
    // 设置给标题的样式
    comStyle() {
      return {
        fontSize: this.titleFontSize + "px",
      };
    },
  },
  created() {
    // 在组件创建完成后，进行回调函数注册
    this.$socket.registerCallBack("trendData", this.getData);
  },
  mounted() {
    // 初始化Charts
    this.initChart();

    // this.getData();
    // 告诉服务器，我现在需要数据
    this.$socket.send({
      action: "getData",
      socketType: "trendData",
      chartName: "trend",
      value: "",
    });
    // 适配
    this.screenAdapter();
    window.addEventListener("resize", this.screenAdapter);
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapter);
    this.$socket.unRegisterCallBack("trendData");
  },
  methods: {
    // res 就是服务端发送给客户端图表的数据
    async getData(res) {
      // const { data: res } = await this.$http.get("trend");
      this.allData = res;
      this.updateChart();
    },
    initChart() {
      this.chartInstance = this.$echarts.init(this.$refs.trend, "chalk");
      // 对图标初始化的控制
      const initOption = {
        // 坐标轴大小设置
        grid: {
          left: "2%",
          top: "30%",
          right: "4%",
          bottom: "2%",
          containLabel: true,
        },
        // 工具提示
        tooltip: {
          trigger: "axis",
          snap: true,
          backgroundColor: "#222733",
          borderColor: "#666666",
          textStyle: {
            color: "#fff",
          },
          axisPointer: {
            // 展示的类型是线条类型
            type: "line",
          },
        },
        // 图例位置和形状
        legend: {
          left: 20,
          top: "15%",
          icon: "circle",
        },
        xAxis: {
          type: "category",
          boundaryGap: false,
        },
        yAxis: {
          type: "value",
        },
      };
      this.chartInstance.setOption(initOption);
    },
    updateChart() {
      const colorArr1 = [
        "rgba(11,168,44,0.5)",
        "rgba(44,110,255,0.5)",
        "rgba(22,242,217,0.5)",
        "rgba(254,33,30,0.5)",
        "rgba(250,105,0,0.5)",
      ];
      const colorArr2 = [
        "rgba(11,168,44,0)",
        "rgba(44,110,255,0)",
        "rgba(22,242,217,0)",
        "rgba(254,33,30,0)",
        "rgba(250,105,0,0)",
      ];

      let timeArr = this.allData.common.month;
      let valueArr = this.allData[this.select.key].data;
      let seriesOption = valueArr.map((item, index) => {
        return {
          name: item.name,
          type: "line",
          data: item.data,
          stack: this.select.key,
          symbolSize: "5",
          areaStyle: {
            color: new this.$echarts.graphic.LinearGradient(0, 0, 0, 1, [
              // 百分之0下的颜色值
              {
                offset: 0,
                color: colorArr1[index],
              },
              // 百分之100下的颜色值
              {
                offset: 1,
                color: colorArr2[index],
              },
            ]),
          },
        };
      });
      // 图例的数据
      let legendArr = valueArr.map((item) => {
        return item.name;
      });
      let dataOption = {
        xAxis: {
          data: timeArr,
        },
        legend: {
          data: legendArr,
        },
        series: seriesOption,
      };
      this.chartInstance.setOption(dataOption);
    },
    // 浏览器大小发生变化会调用此方法
    screenAdapter() {
      this.titleFontSize = (this.$refs.trend.offsetWidth / 100) * 3.6;
      let adapterOption = {
        legend: {
          itemWidth: this.titleFontSize,
          itemHeight: this.titleFontSize,
          itemGap: this.titleFontSize,
          textStyle: {
            fontSize: this.titleFontSize / 2,
          },
        },
      };
      this.chartInstance.setOption(adapterOption);
      this.chartInstance.resize();
    },
    handleCommand(command) {
      this.select = command;
      this.updateChart();
    },
  },
};
</script>

<style lang="less" scoped>
.title {
  position: absolute;
  left: 20px;
  top: 20px;
  z-index: 10;

  .el-dropdown-link {
    color: #fff;
  }
}
</style>
